﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class PricesBL
    {
        public void GetCountByKeyWord(string keyWord, out int totalRecord)
        {
            totalRecord = 0;            
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@KeyWord", keyWord);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Price_Select_Count_By_KeyWord", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }

        public List<Prices> Search(string keyWord, int? order, int pageIndex, int pageSize)
        {
            List<Prices> result = new List<Prices>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@KeyWord", keyWord);
            adapter.AddParameter("@Order", order);           
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<Prices>("sp_Price_Search", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public void GetCountByArtistsId(int artistsId, out int totalRecord)
        {
            totalRecord = 0;
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ArtistsId", artistsId);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Price_Select_Count_By_ArtistsId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }

        public void GetCountPriceHasArtWokByArtistsId(int artistsId, out int totalRecord)
        {
            totalRecord = 0;
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ArtistsId", artistsId);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Price_Select_Count_Price_Has_Art_Work_By_ArtistsId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }

        public List<PricesDetailExtend> GetAllPriceHasArtWorkByArtistsId(int artistsId, int? order, int pageIndex, int pageSize)
        {
            List<PricesDetailExtend> result = new List<PricesDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ArtistsId", artistsId);
            adapter.AddParameter("@Order", order);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<PricesDetailExtend>("sp_Price_Select_All_Price_Has_Art_Work_By_ArtistsId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public List<PricesDetailExtend> GetByArtistsId(int artistsId, int? order, int pageIndex, int pageSize)
        {
            List<PricesDetailExtend> result = new List<PricesDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ArtistsId", artistsId);
            adapter.AddParameter("@Order", order);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<PricesDetailExtend>("sp_Price_Select_By_ArtistsId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public PricesDetailExtend GetById(int id)
        {
            PricesDetailExtend result = new PricesDetailExtend();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Id", id);           
            try
            {
                result = adapter.GetList<PricesDetailExtend>("sp_Price_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }
    }
}
